package com.apexedu.portal.controller;

import com.apexedu.portal.constant.ActionConstant;
import com.apexedu.portal.entity.YxSfLb;
import com.apexedu.portal.entity.YxSfXm;
import com.apexedu.portal.util.DBMUtil;
import com.apexedu.portal.util.LittleTimer;
import com.apexedu.portal.util.YxqhProUtil;
import com.apexedu.util.StringUtil;
import com.apexedu.util.json.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 青海迎新门户
 * Created by ly on 2015/8/11.
 */
@Controller
public class YxQhController extends BaseController {

    /**
     * 初始界面
     *
     * @return 初始界面
     */
    @RequestMapping(value = ActionConstant.YxQh.RequestMapping_init, method = RequestMethod.GET)
    public String init() {
        debug("<<{}>> init start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            return ActionConstant.YxQh.Forward_init;
        } catch (Exception e) {
            error("<<{}>> init error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> init end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 学工报道界面
     *
     * @return 学工报道界面
     */
    @RequestMapping(value = ActionConstant.YxQh.RequestMapping_xgbd, method = RequestMethod.GET)
    public String xgbd(HttpServletRequest request) {
        debug("<<{}>> xgbd start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            String jfqk = request.getParameter("jfqk");//缴费情况类型 0为全部，1为财务，2为学工
            request.setAttribute("jfqk", jfqk);

            String jfname = YxqhProUtil.get("xgdb.jfje.name"); //显示项目
            request.setAttribute("jfname", jfname);
            return ActionConstant.YxQh.Forward_xgbd;
        } catch (Exception e) {
            error("<<{}>> xgbd error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> xgbd end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 获取考生信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = ActionConstant.YxQh.RequestMapping_getUserInfo, method = RequestMethod.GET)
    @ResponseBody
    public String getUserInfo(HttpServletRequest request) {
        debug("<<{}>> getUserInfo start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            String type = request.getParameter("type");
            String value = request.getParameter("value");
            String sql = YxqhProUtil.get("xgbd.student.info.sql");
            if ("sfzh".equals(type)) {
                sql = sql.replaceAll("\\{sfzh\\}", value);
            } else if ("ksh".equals(type)) {
                sql = sql.replaceAll("\\{ksh\\}", value);
            }
            List<Map<String, Object>> list = new DBMUtil("xg").queryForList(null, sql);
            if (list.isEmpty()) {
                return "";
            } else {
                return JsonUtil.getJSONString(list.get(0));
            }
        } catch (Exception e) {
            error("<<{}>> getUserInfo error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getUserInfo end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 获取收费项目信息
     *
     * @param request --
     * @return 获取收费项目信息
     */
    @RequestMapping(value = ActionConstant.YxQh.RequestMapping_getSfxm, method = RequestMethod.GET)
    @ResponseBody
    public String getSfxm(HttpServletRequest request) {
        debug("<<{}>> getSfxm start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            DBMUtil dbmUtil = new DBMUtil("xg");
            String sfzh = request.getParameter("sfzh");
            List<YxSfLb> sfLbList = new ArrayList<YxSfLb>();
            String types = YxqhProUtil.get("xgdb.sfxm.type");
            for (int i = 0; i <= types.split(",").length; i++) {
                String type = "";
                if (i > 0) {
                    type = types.split(",")[i - 1];
                }
                YxSfLb sflb = new YxSfLb(type, i, true);
                YxSfXm sfxm = dbmUtil.queryBean(YxqhProUtil.get("xgdb.jfje.type.sql" + i).replaceAll("\\{sfzh\\}", sfzh), YxSfXm.class);
                sflb.getSfXmList().add(sfxm);
                sfLbList.add(sflb);
                if (i == 0) {
                    continue;
                }

                List<YxSfXm> sfXmList = dbmUtil.queryAllBeanList(YxqhProUtil.get("xgdb.sfxm.type.sql" + i).replaceAll("\\{sfzh\\}", sfzh), YxSfXm.class);
                sfLbList.add(new YxSfLb(type, i, sfXmList));
            }
            return JsonUtil.getJSONString(sfLbList);
        } catch (Exception e) {
            error("<<{}>> getSfxm error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> getSfxm end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }

    /**
     * 完成提交
     *
     * @param request
     */
    @RequestMapping(value = ActionConstant.YxQh.RequestMapping_submit, method = RequestMethod.POST)
    @ResponseBody
    public void submit(HttpServletRequest request) {
        debug("<<{}>> submit start", this.getClass().getSimpleName());
        LittleTimer timer = new LittleTimer();
        try {
            String type = request.getParameter("type"); //类型 0为绿色通道 1为
            String sfzh = request.getParameter("sfzh"); //身份证号
            DBMUtil dbmUtil = new DBMUtil("xg");
            if (!"0".equals(type)) {
                for (int i = 1; i <= 5; i++) {
                    String sql = YxqhProUtil.get("xgbd.submit.type" + type + ".sql" + i);
                    if (StringUtil.isBlank(sql)) {
                        break;
                    }
                    dbmUtil.executeSQL(sql.replaceAll("\\{sfzh\\}", sfzh));
                }
            } else {
                String liststr = request.getParameter("liststr");
                List<YxSfXm> list = JsonUtil.getDTOList(liststr, YxSfXm.class);
                String sql = YxqhProUtil.get("xgdb.submit.type0.sql");//update TYX_SFMX set hjje=?,mjje=? where id =?
                for (YxSfXm sfxm : list) {
                    dbmUtil.executeSQL(sql, sfxm.getHjje(), sfxm.getMjje(), sfxm.getId());
                }
            }
        } catch (Exception e) {
            error("<<{}>> submit error : {}", this.getClass().getSimpleName(), e);
            throw new RuntimeException(e);
        } finally {
            debug("<<{}>> submit end : {}", this.getClass().getSimpleName(), timer.end());
        }
    }
}
